<?php
if(empty($buid))
	msg($config['weburl']."/login.php","请先登录");	
include_once("$config[webroot]/module/product/includes/plugin_orderadder_class.php");
include_once("$config[webroot]/module/product/includes/plugin_cart_class.php");
$cart=new cart();
$orderadder=new orderadder();
//============================直接购买向购物车添加商品信息
if(!empty($_GET['id'])&&!empty($_GET['nums']))
{
	$flag=$cart->add_cart($_GET['id'],$_GET['nums']); 
	if($flag=='error')
		msg($config['weburl']."/?m=product&s=cart","商品不存在");
	if($flag=='error1')
		msg($config['weburl']."/?m=product&s=cart","库存数量不够");
}
//============================读出购物车的数据
$cartlist=$cart->get_listcart();
if(empty($cartlist['sumprice']))
	msg($config['weburl']."/?m=product","购物车没有内容");
//=============================提交订单
if($_POST['act']=='order')
{  
	$_POST['adder']*=1;
    if(empty($_POST['adder']))
		msg($config['weburl']."/?m=product","请检查您是否禁用了,订单提交失败");
    //----------查询送货方式
       $re=$orderadder->get_orderadder($_POST['adder']);
	//----------循环商铺
	foreach($cartlist as $key=>$val)
	{
		$sid=$val['company']['sell_userid'];
		if(!empty($sid))
		{
			//------处理物流
			if(!empty($_POST["cem_$sid"]))
			{
				$logistics=explode(",",$_POST["cem_$sid"]);
			}
			
			$sql = "INSERT INTO ".ORDER." 
			( `buyer_id`, `seller_id`, `buyer_name`, `buyer_addr`, `buyer_tel`,`buyer_mobile`, `buyer_zip`, `product_price`, `logistics_type`, `logistics_price`, `status`, `des`, `creat_time`, `uptime`) 
			VALUES ($buid, $sid, '$re[name]', '$re[province]$re[city]$re[address]', '$re[tel]', '$re[mobile]', '$re[zip]', '".$val['company']['sumprice']."', '$logistics[0]','$logistics[1]',1, '".$_POST['msg_'.$sid]."','".time()."','".time()."')"; 
			$flag=$db->query($sql);
			//-----保存商品信息
			if($flag)
			{    
				$id=$db->lastid();
				foreach($val['prolist'] as $key=>$val)
				{    	
					$sql = "INSERT INTO ".ORPRO." (`order_id`, `buyer_id`, `pid`, `name`, `pic`, `price`, `num`, `time`) 
					VALUES ($id, $buid, $val[pid], '$val[pname]', '".$val['pic'][0]."', '".$val['price']."', '".$val['num']."','".time()."')"; 
						 $db->query($sql);
				}
			}
			else
			    msg($config['weburl'].'/main.php?action=m&m=product&s=admin_buyorder&menu=trade',"订单提交失败");
		}		
	}
	 //------------清空购物车
     $cart->clear_cart();
     msg($config['weburl']."/main.php?action=m&m=product&s=admin_buyorder&menu=trade","订单提交成功");
}		



//=====================================小窗口操作 填加及修改收货地址

//--------------------------读出收货地址
$tpl->assign("listadder",$orderadder->get_orderadderlist());

if(!empty($_POST['submit'])&&(empty($_POST['name'])||empty($_POST['province'])||empty($_POST['city'])||empty($_POST['address'])||empty($_POST['zip'])))
{
	$tpl->assign("close","1");
	echo "<script>alert('参数不正确');</script>";
}
else
{
	//-------------------增加收货地址
	if(!empty($_POST['submit'])&&$_POST['act']=='submit')
	{  
		$flag=$orderadder->add_orderadder(); 
		$tpl->assign("close","1");
		if(!$flag)
			echo "<script>alert('不能超过5条');</script>";  
	}
	//-------------------修改收货地址
	if(!empty($_POST['submit'])&&$_POST['act']=='edit')
	{
		$orderadder->edit_orderadder($_POST['edid']); 
		$tpl->assign("close","1");
	}
}
//-------------------显示收货地址
if(!empty($_GET['adrid'])&&is_numeric($_GET['adrid']))
	$tpl->assign("de",$orderadder->get_orderadder($_GET['adrid']));	
//=================================================
$tpl->assign("config",$config);
if(empty($_GET['showadder']))
{
	$tpl->assign("cart",$cartlist);
	$tpl->assign("current","product");
	include_once("footer.php");
	$out=tplfetch("confirm_order.htm",$flag,true);	
}
else
{
	$tpl->assign("prov",get_province($de['province']));
	$out=tplfetch("add_adder.htm",'',true);
}

?>